#include <iostream>
#include "functions.h"
#include <string>
#include <cstring>
#include <fstream>
#include <chrono>
#include <iomanip> 
#include <ctime>

using namespace std;

struct Contact_information;
struct contact_node;
struct contact_head;
struct contact_tail;

void print_screen();
void add_contact(Contact_information *in_node);
void add_contact();
//declarative section


//implementation section

struct  Contact_information
{
	std::string name;
	std::string phone_number;
	std::string email;
	std::string address;
	std::string sex;
}typedef Contact_information;

struct contact_node
{
	contact_node *next;
	Contact_information data;
}typedef contact_node;

struct contact_head
{
	contact_node *next;
	contact_tail *tail;
	unsigned int  size;
}typedef contact_head;

struct contact_tail
{
	contact_node *prev;
	contact_head *head;
}typedef contact_tail;


contact_head *head = nullptr;
contact_tail *tail = nullptr;


void address_book_init()
{
	print_screen();
	head = new contact_head{nullptr,nullptr,0};
	tail = new contact_tail{nullptr, head};
	head->tail = tail;

	ifstream file("contact_list.txt");
	if(file.is_open())
	{
		string line;
		
		while(getline(file,line))
		{
			Contact_information contact;
			string name, phone_number, email, address, sex;
			int i = 0, j = 0;
			char c;
			while (j<line.length())
			{
				c = line[j];
				if(c == ',')
				{
					i++;
					j++;
					continue;
				}
				if(i == 0)
				{
					name += c;
				}
				else if(i == 1)
				{
					phone_number += c;
				}
				else if(i == 2)
				{
					email += c;
				}
				else if(i == 3)
				{
					address += c;
				}
				else if(i == 4)
				{
					sex += c;
				}
				j++;
			}
			contact.name = name;
			contact.phone_number = phone_number;
			contact.email = email;
			contact.address = address;
			contact.sex = sex;
			add_contact(&contact);
		}
		file.close();
	}
}
void print_screen()
{
	std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
	std::cout << "|| Welcome to Address Book! ||" << std::endl;
	std::cout << "|| 1. Add new contact       ||" << std::endl;
	std::cout << "|| 2. Search contact        ||" << std::endl;
	std::cout << "|| 3. Delete contact        ||" << std::endl;
	std::cout << "|| 4. Update contact        ||" << std::endl;
	std::cout << "|| 5. display_all_contacts  ||" << std::endl;
	std::cout << "|| 6. Delete all contacts   ||" << std::endl;
	std::cout << "|| 7. Exit                  ||" << std::endl;
	std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
}
void add_contact(Contact_information *in_node)
{
		contact_node *new_node = new contact_node;

		new_node->data.name = in_node->name;
		new_node->data.phone_number = in_node->phone_number;
		new_node->data.email = in_node->email;
		new_node->data.address = in_node->address;
		new_node->data.sex = in_node->sex;
		new_node->next = nullptr;

	if(head->size == 0)
	{
		tail->prev = head->next = new_node;
	}
	else
	{
		tail->prev->next = new_node;
		tail->prev = new_node;
	}
	head->size++;
}
void add_contact()
{
		contact_node *new_node = new contact_node;
		std::cout << "please enter your contact name:"<< std::endl;
		std::cin >> new_node->data.name;
		std::cout << "please enter your contact phone number:"<< std::endl;
		std::cin >> new_node->data.phone_number;
		std::cout << "please enter your contact email:"<< std::endl;
		std::cin >>	new_node->data.email;
		std::cout << "please enter your contact address:"<< std::endl;
		std::cin >>	new_node->data.address;
		std::cout << "please enter your contact sex:"<< std::endl;
		std::cin >>	new_node->data.sex;
		new_node->next = nullptr;

	if(head->size == 0)
	{
		tail->prev = head->next = new_node;
	}
	else
	{
		tail->prev->next = new_node;
		tail->prev = new_node;
	}
	head->size++;
}
void search_contact()
{
	std::cout << "Who will you search for?" << std::endl;
	std::string name;
	std::cin >> name;
	contact_node *current = head->next;
	while (current != nullptr)
	{
		if(current->data.name == name)
		{
			std::cout << "Name: " << current->data.name << "\t";
			std::cout << "Phone Number: " << current->data.phone_number << "\t";
			std::cout << "Email: " << current->data.email << "\t";
			std::cout << "Address: " << current->data.address << "\t";
			std::cout << "Sex: " << current->data.sex << "\t";
			std::cout << "~" << std::endl;
			return;
		}
		current = current->next;
	} 
	std::cout << "Contact not found" << std::endl;
}
void delete_contact()
{
	if(head->size == 0)
	{
		std::cout << "No contacts found" << std::endl;
		return;
	}

	std::cout << "Who will you delete?" << std::endl;
	std::string name;
	std::cin >> name;

	contact_node *this_prev = head->next;
	contact_node *this_current = head->next->next;

	if(head->size == 1 &&name == head->next->data.name)
	{
		delete head->next;
		head->next = nullptr;
		tail->prev = nullptr;
		head->size--;
		std::cout << "deleted successfully" << std::endl;
		return;
	}
	if(name == head->next->data.name && head->size > 1&& head->next->next != nullptr)
	{
		delete head->next;
		head->next = this_current;
		head->size--;
		
		std::cout << "deleted successfully" << std::endl;
		return;
	}
	

	while (this_current != nullptr)
	{
		if(this_current->data.name == name)
		{
			//delete current;
			if(this_current->next == nullptr)
			{
				this_prev->next = nullptr;
				tail->prev = this_prev;	
			}
			else
			{
				this_prev->next = this_current->next;
			}

			head->size--;
			std::cout << "deleted successfully" << std::endl;
			delete this_current;
			return;
		}
		this_current = this_current->next;
		this_prev = this_prev->next;
	} 
	std::cout << "Contact not found" << std::endl;
}
void update_contact()
{
std::cout << "Who will you update?" << std::endl;
	std::string name;
	std::cin >> name;
	contact_node *current = head->next;
	while (current != nullptr)
	{
		if(current->data.name == name)
		{
			std::cout << "name: " << std::endl;
			std::cin >> current->data.name;
			std::cout << "phone number: " << std::endl;
			std::cin >> current->data.phone_number;
			std::cout << "email: " << std::endl;
			std::cin >> current->data.email;
			std::cout << "address: " << std::endl;
			std::cin >> current->data.address;
			std::cout << "sex: " << std::endl;
			std::cin >> current->data.sex;
			std::cout << "updated successfully" << std::endl;
			return;
		}
		current = current->next;
	} 
	std::cout << "Contact not found" << std::endl;
}
void display_all_contacts()
{
	if(head->size == 0)
	{
		std::cout << "No contacts found" << std::endl;
		return;
	}
	contact_node *current = head->next;
	while (current != nullptr)
	{
		std::cout << "Name: " << current->data.name << "\t";
		std::cout << "Phone Number: " << current->data.phone_number << "\t";
		std::cout << "Email: " << current->data.email << "\t";
		std::cout << "Address: " << current->data.address << "\t";
		std::cout << "Sex: " << current->data.sex << "\t";
		std::cout << "~" << std::endl;
		current = current->next;
	} 
}
void delete_all_contacts()
{
	if(head->size == 0)
	{
		std::cout << "contact list is empty" << std::endl;
		
		return;
	}

	contact_node *current = head->next;

	if (head->size == 1)
	{
	delete head->next;
	head->next = nullptr;
	tail->prev = nullptr;
	head->size = 0;
	std::cout << "contact done successfully" << std::endl;
	return;
	}
	
	contact_node *next_node = current->next;
	
	while (next_node != nullptr)
	{
		next_node = next_node->next;
		delete current;
		current = next_node;
	}
	head->next = nullptr;
	tail->prev = nullptr;
	head->size = 0;
	std::cout << "contact done successfully" << std::endl;
}
void edit_contact()
{
	ofstream file;
	file.open("contact_list.txt", ios::out | ios::trunc);
	if (!file.is_open())
	{
	ofstream error_m;
	error_m.open("error_log.txt", ios::app);
	if (!error_m.is_open())
	{
		    // 获取当前时间点
    auto now = std::chrono::system_clock::now();
    
    // 将时间点转换为time_t类型
    std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
    
    // 将time_t类型转换为本地时间
    std::tm* now_tm = std::localtime(&now_time_t);
    
    // 输出格式化的时间字符串
     
	error_m << "Error opening file" << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S")<<std::endl;
	return;
	}
	}
	contact_node *current = head->next;
	while (current != nullptr)
	{
		file << current->data.name << "," << current->data.phone_number << "," << current->data.email << "," << current->data.address << "," << current->data.sex << std::endl;
		current = current->next;
	}
	file.close();

	std::cout << "Edit contact" << std::endl;
	std::cout << "thanks for using our service" << std::endl;
} 